﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.ComponentModel;
using NoteList.Delegates;
using NoteList.EvtArgs;
using NoteList.Models;
using System.Xml.Serialization;
using System.IO;

namespace NoteList.Biz
{
    public class ZNoteProvider
    {
        public event NoteLoadCompleted OnNoteLoadCompleted;
        public void AsynLoadNotes()
        {
            BackgroundWorker bwLoadNotes = new BackgroundWorker();
            bwLoadNotes.DoWork += new DoWorkEventHandler(bwLoadNotes_DoWork);
            bwLoadNotes.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwLoadNotes_RunWorkerCompleted);
            bwLoadNotes.RunWorkerAsync();
        }

        void bwLoadNotes_DoWork(object sender, DoWorkEventArgs e)
        {
            ZNotes znotes = LoadNotes();
            e.Result = znotes;
        }

        void bwLoadNotes_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            NotesEventArgs me = new NotesEventArgs();
            me.Notes = (ZNotes)e.Result;
            OnNoteLoadCompleted.Invoke(this, me);
        }

        public event NoteSaveCompleted OnNoteSaveCompleted;
        public void AnsySaveNotes(ZNotes notes)
        {
            BackgroundWorker bwSaveNotes = new BackgroundWorker();
            bwSaveNotes.DoWork += new DoWorkEventHandler(bwSaveNotes_DoWork);
            bwSaveNotes.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwSaveNotes_RunWorkerCompleted);
            bwSaveNotes.RunWorkerAsync(notes);
        }

        void bwSaveNotes_DoWork(object sender, DoWorkEventArgs e)
        {
            ZNotes notes = (ZNotes)e.Argument;
            SaveNotes(notes);
        }

        void bwSaveNotes_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            NoteSaveEventArgs me = new NoteSaveEventArgs();
            if (e.Cancelled)
            {
                me.HasError = true;
                me.ErrMsg = "Cancelled";
            }
            else
            {
                me.HasError = (e.Error != null);
                me.ErrMsg = (e.Error == null) ? "" : e.Error.Message;
            }
            OnNoteSaveCompleted.Invoke(this, me);
        }

        private ZNotes LoadNotes()
        {
            ZNotes notes = null;
            if (File.Exists("note.dat"))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(ZNotes));
                try
                {
                    FileStream fs = new FileStream("note.dat", FileMode.Open);
                    notes = (ZNotes)serializer.Deserialize(fs);
                    fs.Close();
                }
                catch (Exception exp)
                {
                    System.Diagnostics.Trace.WriteLine(exp.StackTrace);
                    notes = new ZNotes();
                }
            }
            else
            {
                notes = new ZNotes();
            }
            return notes;
        }

        private void SaveNotes(ZNotes notes)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ZNotes));
            TextWriter writer = new StreamWriter("note.dat");
            serializer.Serialize(writer, notes);
            writer.Close();
        }
    }
}
